package cn.hongwei.mall.pms.service.impl;

import cn.hongwei.mall.pms.entity.AttrEntity;
import cn.hongwei.mall.pms.mapper.AttrMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hongwei.mall.common.bean.PageResultVo;
import cn.hongwei.mall.common.bean.PageParamVo;

import cn.hongwei.mall.pms.mapper.SkuAttrValueMapper;
import cn.hongwei.mall.pms.entity.SkuAttrValueEntity;
import cn.hongwei.mall.pms.service.SkuAttrValueService;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;


@Service("skuAttrValueService")
public class SkuAttrValueServiceImpl extends ServiceImpl<SkuAttrValueMapper, SkuAttrValueEntity> implements SkuAttrValueService {

    @Autowired
    private AttrMapper attrMapper;

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<SkuAttrValueEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<SkuAttrValueEntity>()
        );

        return new PageResultVo(page);
    }

    @Override
    public List<SkuAttrValueEntity> querySearchAttrValuesBySkuIdAndCid(Long skuId, Long cid) {
        //查询检索类型的规格参数
        List<AttrEntity> attrEntities = attrMapper.selectList(new LambdaQueryWrapper<AttrEntity>().eq(AttrEntity::getCategoryId, cid).eq(AttrEntity::getSearchType,1));
        if(CollectionUtils.isEmpty(attrEntities)){
            return null;
        }
        //查询当前商品对应的规格参数和值
        List<Long> attrIds = attrEntities.stream().map(AttrEntity::getId).collect(Collectors.toList());
        //查询当前商品对应的规格参数和值
        return baseMapper.selectList(new LambdaQueryWrapper<SkuAttrValueEntity>().eq(SkuAttrValueEntity::getSkuId, skuId).in(SkuAttrValueEntity::getAttrId, attrIds));
    }

}